<?php

class ContactForm extends BaseForm {

    public function __construct($suffix = null, $number = 1) {
        $this->index = ($suffix) ? $suffix : '';
        $this->number = $number;
        parent::__construct();
    }

    public function setup() {
        $titles = $this->getTitlesFormatted();

        $this->setWidgets(array(
            'title_id'. $this->index => new sfWidgetFormChoice(array('choices' => $titles)),
            'name'. $this->index => new sfWidgetFormInputText(),
            'email'. $this->index => new sfWidgetFormInputText(),
            'position'. $this->index => new sfWidgetFormInputText(),
        ));


        $this->setValidators(array(
            'title_id'. $this->index => new sfValidatorChoice(array('choices' => array_keys($titles), 'required' => false), array('invalid' => 'Invalid Title.', 'required' => 'Please enter required field.')),
            'name'. $this->index => new sfValidatorString(array('required' => false), array('required' => 'Please enter required field.')),
            'position'. $this->index => new sfValidatorString(array('required' => false), array('required' => 'Please enter required field.')),
            'email'. $this->index => new sfValidatorRegex(array('required' => false, 'pattern' => '/^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'), array('invalid' => 'Invalid E-mail.', 'required' => 'Please enter required field.')),
        ));

        for ($i = 0; $i < $this->number; $i++) {
            $this->mergeForm(new PhoneForm($this->index.'_' . $i));
        }

        $this->widgetSchema->setNameFormat('contact[%s]');
    }

    private function getTitlesFormatted() {
        $bank_accounts = Doctrine_Query::create()->from('Title')->fetchArray();
        $result = array('' => '');

        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['title_name'];
        }
        return $result;
    }

}

?>
